@model WorkFlowCore.BusinessDemo.Web.Models.PagedResult<WorkFlowCore.BusinessDemo.Web.Models.OfficialSealApplicationViewModel>
@using WorkFlowCore.BusinessDemo.Web.Entities // Required for WorkTaskStatus enum comparison

@{
    ViewData["Title"] = "公章申请列表";
}

<h2>@ViewData["Title"]</h2>

<p>
    <a asp-action="Create" class="btn btn-primary">创建新申请</a>
    <a asp-action="SubmitForApprovalDoc" class="btn btn-primary ml-2">查看发起审批文档</a>
</p>

<form asp-action="Index" method="get" class="mb-3">
    <div class="input-group">
        <input type="text" name="searchTerm" class="form-control" placeholder="搜索申请人或事由" value="@ViewBag.SearchTerm">
        <div class="input-group-append">
            <button class="btn btn-outline-secondary" type="submit">搜索</button>
        </div>
    </div>
</form>

<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Items.First().ApplicantName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items.First().ApplicantDepartment)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items.First().ApplicationDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items.First().SealType)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items.First().Reason)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items.First().WorkTaskId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items.First().CurrentWorkTaskStatusDisplay)
            </th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model.Items) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ApplicantName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ApplicantDepartment)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ApplicationDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SealType)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Reason)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.WorkTaskId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CurrentWorkTaskStatusDisplay)
            </td>
            <td>
                @{
                    bool canEdit = string.IsNullOrEmpty(item.WorkTaskId) || item.CurrentWorkTaskStatus == WorkTaskStatus.Pending.ToString();
                    bool canDelete = string.IsNullOrEmpty(item.WorkTaskId) || item.CurrentWorkTaskStatus == WorkTaskStatus.Pending.ToString();
                    bool canSubmit = string.IsNullOrEmpty(item.WorkTaskId) || item.CurrentWorkTaskStatus == WorkTaskStatus.Canceled.ToString() || item.CurrentWorkTaskStatus == WorkTaskStatus.Pending.ToString();
                }

                @if (canEdit)
                {
                    <a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-sm btn-outline-primary">编辑</a>
                }
                else
                {
                    <button type="button" class="btn btn-sm btn-outline-primary" disabled>编辑</button>
                }
                <text> | </text>
                <a asp-action="Details" asp-route-id="@item.Id" class="btn btn-sm btn-outline-info">详情</a>
                <text> | </text>
                @if (canDelete)
                {
                    <a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-sm btn-outline-danger">删除</a>
                }
                else
                {
                    <button type="button" class="btn btn-sm btn-outline-danger" disabled>删除</button>
                }

                @if (canSubmit)
                {
                    <form asp-action="SubmitForApproval" asp-route-id="@item.Id" method="post" style="display:inline; margin-left: 5px;">
                        @Html.AntiForgeryToken()
                        <button type="submit" class="btn btn-sm btn-outline-success" onclick="return confirm('确定要提交此申请 (#@item.Id) 进行审批吗？');">提交审批</button>
                    </form>
                    
                    <form asp-action="SubmitForApprovalWithPublicLink" asp-route-id="@item.Id" method="post" style="display:inline; margin-left: 5px;">
                        @Html.AntiForgeryToken()
                        <button type="submit" class="btn btn-sm btn-outline-primary" onclick="return confirm('确定要提交此申请 (#@item.Id) 进行审批吗？（将使用公开链接）');">提交审批（公开链接）</button>
                    </form>
                }
                else if (!string.IsNullOrEmpty(item.WorkTaskId)) // Already submitted
                {
                    // Display current status or a generic "已提交" message
                    <button type="button" class="btn btn-sm btn-secondary" disabled style="margin-left: 5px;">
                        @(item.CurrentWorkTaskStatusDisplay ?? "已提交")
                    </button>
                }
            </td>
        </tr>
}
    </tbody>
</table>

@{
    var prevDisabled = Model.PageIndex <= 1 ? "disabled" : "";
    var nextDisabled = Model.PageIndex >= (int)Math.Ceiling((double)Model.TotalCount / Model.PageSize) ? "disabled" : "";
}

<nav aria-label="Page navigation">
    <ul class="pagination">
        <li class="page-item @prevDisabled">
            <a class="page-link" asp-action="Index"
               asp-route-pageNumber="@(Model.PageIndex - 1)"
               asp-route-pageSize="@Model.PageSize"
               asp-route-searchTerm="@ViewBag.SearchTerm">
                上一页
            </a>
        </li>
        @for (int i = 1; i <= (int)Math.Ceiling((double)Model.TotalCount / Model.PageSize); i++)
        {
            <li class="page-item @(i == Model.PageIndex ? "active" : "")">
                <a class="page-link" asp-action="Index"
                   asp-route-pageNumber="@i"
                   asp-route-pageSize="@Model.PageSize"
                   asp-route-searchTerm="@ViewBag.SearchTerm">@i</a>
            </li>
        }
        <li class="page-item @nextDisabled">
            <a class="page-link" asp-action="Index"
               asp-route-pageNumber="@(Model.PageIndex + 1)"
               asp-route-pageSize="@Model.PageSize"
               asp-route-searchTerm="@ViewBag.SearchTerm">
                下一页
            </a>
        </li>
    </ul>
</nav>
